package com.java.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import com.java.utils.UserSession;

/**
* @ClassName: BaseJFrame
* @Description: 继承JFrame实现自己的框架，可用于自定义一些共用操作
* @author ChenJiaLu
* @date 2023年12月29日 上午9:51:11
*
 */
public class BaseJFrame extends JFrame {
	private static final long serialVersionUID = 5602570535905658078L;

	public JFrame bJFrame;

	public UserSession session = UserSession.getInstance();

	public void initMenu(JFrame frame) {
		this.bJFrame = frame;
		/**
		 * 创建整个系统的菜单栏和菜单项
		 */
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		/**
		 * 创建管理员菜单
		 */
		JMenu adminMenu = new JMenu("管理员操作");
		JMenuItem addDoctorItem = new JMenuItem("添加医生");
		JMenuItem doctorItem = new JMenuItem("医生信息维护");
		JMenuItem adminPswItem = new JMenuItem("密码修改");
		if (!session.isAdmin()) {
			addDoctorItem.setEnabled(false);
			doctorItem.setEnabled(false);
			adminPswItem.setEnabled(false);
		}
		adminMenu.add(addDoctorItem);
		adminMenu.add(doctorItem);
		adminMenu.add(adminPswItem);
		menuBar.add(adminMenu);

		addDoctorItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setEnabled(false);
				new AddDoctorWindow(bJFrame, null);
			}
		});

		doctorItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setVisible(false);
				new AdminMainWindow();
			}
		});

		adminPswItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setEnabled(false);
				new AdminPasswordWindow(bJFrame);
			}
		});

		/**
		 * 创建医生菜单
		 */
		JMenu doctorMenu = new JMenu("医生操作");
		JMenuItem addPatientItem = new JMenuItem("添加患者");
		JMenuItem patientItem = new JMenuItem("维护患者信息");
		JMenuItem orderItem = new JMenuItem("预约管理");
		JMenuItem commentItem = new JMenuItem("留言管理");
		if (!session.isDoctor()) {
			addPatientItem.setEnabled(false);
			patientItem.setEnabled(false);
			orderItem.setEnabled(false);
			commentItem.setEnabled(false);
		}
		doctorMenu.add(addPatientItem);
		doctorMenu.add(patientItem);
		doctorMenu.add(orderItem);
		doctorMenu.add(commentItem);
		menuBar.add(doctorMenu);

		addPatientItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setEnabled(false);
				new AddPatientWindow(bJFrame, null);
			}
		});

		patientItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setVisible(false);
				new DoctorMainWindow();
			}
		});

		orderItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setVisible(false);
				new DoctorOrderWindow();
			}
		});

		commentItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setVisible(false);
				new DoctorCommentWindow();
			}
		});

		/**
		 * 创建患者菜单
		 */
		JMenu patientMenu = new JMenu("患者操作");
		JMenuItem viewDoctorsItem = new JMenuItem("查看医生");
		JMenuItem myOrderItem = new JMenuItem("我的预约");
		JMenuItem myMessageItem = new JMenuItem("我的留言");
		if (!session.isPatient()) {
			viewDoctorsItem.setEnabled(false);
			myOrderItem.setEnabled(false);
			myMessageItem.setEnabled(false);
		}
		patientMenu.add(viewDoctorsItem);
		patientMenu.add(myOrderItem);
		patientMenu.add(myMessageItem);
		menuBar.add(patientMenu);

		viewDoctorsItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setVisible(false);
				new PatientMainWindow();
			}
		});

		myOrderItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setVisible(false);
				new MyOrderWindow();
			}
		});

		myMessageItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setVisible(false);
				new MyCommentWindow();
			}
		});

		// 共用菜单
		JMenu commonMenu = new JMenu("个人中心");
		JMenuItem mine = new JMenuItem("我的信息");
		JMenuItem logout = new JMenuItem("退出登陆");
		JMenuItem logoutexit = new JMenuItem("退出系统");
		JMenuItem aboutSys = new JMenuItem("关于");
		logout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logoutSys();
			}
		});
		logoutexit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exitSys();
			}
		});
		aboutSys.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ok("欢迎使用");
			}
		});
		mine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ok("登陆账号：" + session.getUserName());
			}
		});
		commonMenu.add(mine);
		commonMenu.add(logout);
		commonMenu.add(logoutexit);
		commonMenu.add(aboutSys);
		menuBar.add(commonMenu);

	}
	
	public void logoutSys() {
		if (confirm("是否退出系统登录")) {
			session.logout();
			bJFrame.dispose();
			new LoginWindow();
		}
	}
	
	public void exitSys() {
		if (confirm("是否关闭系统")) {
			System.exit(0);
		}
	}

	public void ok(String msg) {
		JOptionPane.showMessageDialog(bJFrame, msg, "提示", JOptionPane.INFORMATION_MESSAGE);
	}

	public void error(String msg) {
		JOptionPane.showMessageDialog(bJFrame, msg, "提示", JOptionPane.ERROR_MESSAGE);
	}

	public void warning(String msg) {
		JOptionPane.showMessageDialog(bJFrame, msg, "提示", JOptionPane.WARNING_MESSAGE);
	}

	public void loading(String msg) {
		JOptionPane.showMessageDialog(bJFrame, msg, "加载中", JOptionPane.PLAIN_MESSAGE);
	}

	public boolean confirm(String msg) {
		int optionType = JOptionPane.YES_NO_OPTION; // 确认框的选项类型
		int messageType = JOptionPane.QUESTION_MESSAGE; // 消息类型（警告、信息、询问等）
		int result = JOptionPane.showConfirmDialog(null, msg, "确认提示", optionType, messageType); // 显示确认框并返回结果
		return result == JOptionPane.YES_OPTION;
	}

}
